package com.chen.exception;

import com.chen.util.Result;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice

// 等价 @RestController + ControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 处理自定义异常 (我手动抛出的)
     * @param businessException
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public Result error(BusinessException businessException){
        businessException.printStackTrace();
        return  Result.build(null,businessException.getCode(),businessException.getMessage());
    }


    /**
     *  处理系统异常(意想不到的异常)
     * @param e
     * @return
     */
//    加上这个注解,spring会自动调用
    @ExceptionHandler(Exception.class)
    public Result error(Exception e){
        e.printStackTrace();
        return Result.build(null,500,e.getMessage());

    }
}